<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="core.*,util.*" %>
<%@ page import="java.util.*" %>
<%@ include file="/WEB-INF/include/checkAdmin.jsp" %>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/WEB-INF/include/header.jsp" %>
<style>
    div.form-inline { margin-bottom: 5px; }
    table.table th:first-child { width: 20px; }
    td:nth-child(2) input { width: 250px; }
    td:nth-child(3) input { width: 150px; }
    tbody tr:hover { background-color: #FFC; cursor: pointer; }
</style>
<script>
$(function() {
    $("select[name=od]").change(function() {
        $("form").submit();
    })
    $("tbody tr:not(:last-child)").click(function() {
        var queryString = location.search.replace(/\?/g, "");
        location.href = "userGroupEdit.jsp?id=" + $(this).attr("data-id") + "&" + queryString;
    })
})
</script>
</head>
<%
String errMsg = null, infoMsg = null;
RequestParameter param = new RequestParameter(request);
int order = param.getInt("od", 0);
String title = param.getString("title", "");
String category = param.getString("category", "");
String cmd = param.getString("cmd", "");

if ("추가".equals(cmd)) {
    if (title.length() >= 2) {
        if (category.length() >= 2) {
            UserGroupDAO.insert(title, category);
            infoMsg = "저장되었습니다";
        }
        else errMsg = "분류가 바르지 않습니다";
    } 
    else errMsg = "제목이 바르지 않습니다";
}
ArrayList<UserGroup> list = UserGroupDAO.selectAllWithCount(order);
%>
<body>
<%@ include file="/WEB-INF/include/menu.jsp" %>

<div class="container main">

    <h1>사용자 그룹 목록</h1>
    <hr />

    <form method="post">
        <div class="form-inline">
            정렬:
            <select name="od">
                <option value="0" <%= order==0 ? "selected" : "" %>>분류</option>
                <option value="1" <%= order==1 ? "selected" : "" %>>제목</option>
            </select>
        </div>

        <table class="table table-condensed table-bordered">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>제목</th>
                    <th>분류</th>
                    <th>인원</th>
                </tr>
            </thead>
            <tbody>
                <% for (UserGroup group : list) { %>
                    <tr data-id="<%=group.getId()%>">
                        <td><%=group.getId()%></td>
                        <td><%= group.getName() %></td>
                        <td><%= group.getCategory() %></td>
                        <td><%= group.getCount() %></td>
                    </tr>
                <% } %>
                <tr>
                    <td></td>
                    <td><input type="text" name="title" /></td>
                    <td><input type="text" name="category" /></td>
                    <td><button class="btn" name="cmd" value="추가"><i class="icon-plus"></i>
                        사용자그룹 추가</button></td>
                </tr>
            </tbody>
        </table>
    </form>

    <% if (errMsg != null) { %>
        <div class="alert alert-error"><%= errMsg %></div>
    <% } %>
    <% if (infoMsg != null) { %>
        <div class="alert alert-info"><%= infoMsg %></div>
    <% } %>

</div>

<%@ include file="/WEB-INF/include/footer.jsp" %>
</body>
</html>
